package com.qf.mvc2202.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/pick")
public class PickNameController {

    @RequestMapping("/test1")
    public String pickName(HttpServletRequest req, HttpSession session) throws IOException {
        InputStream resourceAsStream = getClass().getResourceAsStream("/names.txt");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resourceAsStream,"utf-8"));
        List<String> list = new ArrayList<>();
        String line = null;
        while((line=bufferedReader.readLine())!=null){
            list.add(line);
        }
        Integer index = (int)(Math.random()*list.size());
        String name = list.get(index);
        session.setAttribute("name",name);
        return "/pickname.jsp";
    }
}
